//
//  IndexMarketOnlineView.m
//  YunFu
//
//  Created by Bager on 2018/8/31.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "IndexMarketOnlineView.h"

#import "IndexTwoItemSizeCell.h"
#import "IndexMarketCell.h"

@implementation IndexMarketOnlineView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = HexColor(0xf4f5f6);
        [self createView];
        [self createTitleView];
    }
    return self;
}

- (void)createTitleView
{
    UILabel *labelDown = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 45, 5, 90, 30)];
    labelDown.textAlignment = NSTextAlignmentCenter;
    labelDown.text = @"在线商城";
    labelDown.font = [UIFont systemFontOfSize:14];
    labelDown.backgroundColor = HexColor(0xfb7d7d);
    labelDown.layer.cornerRadius = 14;
    labelDown.layer.masksToBounds = YES;
    labelDown.adjustsFontSizeToFitWidth = YES;
    labelDown.textColor = [UIColor whiteColor];
    [self addSubview:labelDown];
}

- (void)createView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的大小，
//    flowLayout.itemSize = CGSizeMake((kScreenWidth - kPublicMargin * 2) / 2.0, 80);
    // 设置列的最小间距
    flowLayout.minimumInteritemSpacing = 0;
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 0;
    // 设置布局的内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.marketCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(kPublicMargin, 20, kScreenWidth - kPublicMargin * 2, 80 * 2 + 20)  collectionViewLayout:flowLayout];
    
    self.marketCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.marketCollectionView.dataSource = self;
    
    self.marketCollectionView.delegate = self;
    
    [self.marketCollectionView registerClass:[IndexTwoItemSizeCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [self.marketCollectionView registerClass:[IndexMarketCell class] forCellWithReuseIdentifier:@"cellIdentifiers"];

    
    self.marketCollectionView.layer.cornerRadius = 4;
    self.marketCollectionView.layer.masksToBounds = YES;
    
    [self addSubview:self.marketCollectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (CGSize) collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
   sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item > 1)
    {
        return CGSizeMake((kScreenWidth - kPublicMargin * 2) / 2.0, 100);
    }
    else
    {
        return CGSizeMake((kScreenWidth - kPublicMargin * 2) / 2.0, 80);
    }
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item > 1)
    {
        IndexMarketCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifiers" forIndexPath:indexPath];
        
        NSArray *imgArray = @[@"yf1-sp1",@"yf1-sp3"];
        NSArray *imgOArray = @[@"yf1-sp2",@"yf1-sp4"];
        NSArray *nameArray = @[@"有好货",@"逛好店"];
        NSArray *detailArray = @[@"品质生活出租屋也可以有家的感觉",@"商圈好店全部掌握"];
        NSArray *colorArray = @[HexColor(0x0d95eb),HexColor(0xa137ff)];
        
        [cell.img setImage:[UIImage imageNamed:imgArray[indexPath.item - 2]]];
        [cell.imgO setImage:[UIImage imageNamed:imgOArray[indexPath.item - 2]]];
        cell.name.text = nameArray[indexPath.item - 2];
        cell.name.textColor = colorArray[indexPath.item - 2];
        cell.detail.text = detailArray[indexPath.item - 2];
        
        return cell;
    }
    else
    {
        IndexTwoItemSizeCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        
        NSArray *imgArray = @[@"yf1-sc1",@"yf1-sc2"];
        NSArray *nameArray = @[@"天天特价",@"线上商城"];
        
        [cell.img setImage:[UIImage imageNamed:imgArray[indexPath.item]]];
        cell.name.text = nameArray[indexPath.item];
        
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
